/**
 * 
 */
package com.shequan.shequan.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ailk.dazzle.util.data.JSONUtils;
import com.shequan.activity.bean.pojo.Activity;
import com.shequan.activity.bean.pojo.ActivityNews;
import com.shequan.activity.bean.pojo.ActivityNewsComment;
import com.shequan.activity.service.ActivityNewsCommentService;
import com.shequan.activity.service.ActivityNewsService;
import com.shequan.activity.service.ActivityService;
import com.shequan.common.bean.ResultDataStruct;
import com.shequan.common.bean.ViewTextConstans;
import com.shequan.common.bean.WebConstants;
import com.shequan.shequan.bean.bo.ActivityInfo;
import com.shequan.shequan.bean.pojo.Shequan;
import com.shequan.shequan.service.ShequanService;
import com.shequan.user.bean.bo.UserSession;

/**
 * <P>
 * Copyright 2012.
 * </p>
 * <P>
 * All Rights Reserved.
 * </p>
 * 
 * @author YuJF
 * @version 1.0, 2012-3-14
 * @see com.shequan.shequan.controller.ShequanNewsController
 * @since 1.0
 */
@Controller
@RequestMapping("/shequan")
public class ShequanNewsController {
	private static Logger log = Logger.getLogger(ShequanNewsController.class);

	private ShequanService shequanService;
	private ActivityNewsService activityNewsService;
	private ActivityNewsCommentService activityNewsCommentService;
	private ActivityService activityService;

	public void setActivityNewsCommentService(
			ActivityNewsCommentService activityNewsCommentService) {
		this.activityNewsCommentService = activityNewsCommentService;
	}

	public void setActivityNewsService(ActivityNewsService activityNewsService) {
		this.activityNewsService = activityNewsService;
	}

	public void setShequanService(ShequanService shequanService) {
		this.shequanService = shequanService;
	}

	/**
	 * 查询用户最新活动动态及评论、最新活动及评论
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/activity/news")
	public void getShequanActivityInfo(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ActivityInfo activityInfo = new ActivityInfo();

		ResultDataStruct<ActivityInfo> rds = new ResultDataStruct<ActivityInfo>();
		ResultDataStruct<ActivityNews> newsRDS;
		ResultDataStruct<ActivityNewsComment> newsCommentRDS = null;
		ResultDataStruct<Activity> activityRDS;

		log.debug("---in getShequanActivityInfo---");
		UserSession userSession = (UserSession) request.getSession()
				.getAttribute(WebConstants.USER_SESSION);
		int userId;
		try {
			userId = userSession.getUserId();
			// userId=1;
			// 查询活动动态
			newsRDS = activityNewsService
					.queryLastestActivityNewsByUserId(userId);
			if (newsRDS.isBusiSuccess()) {
				activityInfo.setActivityNews(newsRDS.getData());
				// 查询活动动态评论
				newsCommentRDS = activityNewsCommentService
						.queryByNewsId(activityInfo.getActivityNews()
								.getActivityNewsId());
			}
			if(newsCommentRDS.isBusiSuccess()){
				//查询活动动态评论回复
				//待完成
				activityInfo.setActivityNewsComment(newsCommentRDS.getData());
			}
			// 查询活动
			activityRDS = activityService.queryLastestActivityByUserId(userId);
			if(activityRDS.isBusiSuccess()){
				activityInfo.setActivity(activityRDS.getData());
				
			}
			rds.setData(activityInfo);
			rds.setBusiSuccess(true);
			rds.setSuccess(true);
		} catch (Exception e) {
			log.error("", e);
			rds.setSuccess(false);
			rds.setMessage(ViewTextConstans.UNKNOW_ERROR_MSG);
		}
		String jsonData = JSONUtils.bean2JsonString(rds);
		response.getWriter().print(jsonData);
	}
}
